/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package users.tag;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import tracker.Torrents;
import tracker.TrackerHelper;
import tracker.Users;

/**
 *
 * @author El Desperado
 */
public class ListTorrentsTagHandler extends TagSupport {

    @Override
    public int doStartTag() throws JspException {
        return super.doStartTag();
    }

    @Override
    public int doEndTag() throws JspException {

        String dataString = "";
        try {
            TrackerHelper helper = new TrackerHelper();
            Collection<Torrents> list = helper.getAllTorrents();
            Iterator<Torrents> it = list.iterator();
            while (it.hasNext()) {
                Torrents tor = it.next();
                dataString += "<li>";
                dataString += "<a href=\"../singleTorrent.jsp?torrentId=" + tor.getTorrentId()
                        + "\"> <h3>" + tor.getTorrentName() + "</h3></a>" + "<div id=\"torrentDate\"><a>" + tor.getTorrentTime() + "</a></div>"; //Link to specific torrent info page

                dataString += "<h4>" + tor.getTorrentDescription() + "</h4>";
                dataString += ("</li>");
            }
        } catch (Exception e) {
            System.out.println("Error in ListTorrentsTag: " + e.getMessage());
        }
        try {
            pageContext.getOut().write(dataString);
        } catch (IOException ex) {
            Logger.getLogger(ListTorrentsTagHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return EVAL_PAGE;
    }
}
